﻿using UnityEngine;
using System.Collections;

enum Direction
{
	Up,
	Down,
	Left,
	Right
};

public class TankMovement : MonoBehaviour 
{
	Vector3  DIRECTION_RIGHT =  Vector3.right;
	Vector3  DIRECTION_LEFT= Vector3.left;
	Vector3  DIRECTION_UP = Vector3.up;
	Vector3  DIRECTION_DOWN = Vector3.down;

	public Vector3 m_direction;
	public float m_speed;
	public bool m_isAlive;
	public bool m_isBlocked;
	private float m_rotationAngle;
	private float m_recoilTime = 0.5f;
	public Missile m_missileObject;
	public float m_missileSpeed;
	public Missile[] m_missilePod = new Missile[10];
	private float m_tileWidth;
	private float m_tileHeight;
	// Use this for initialization
	void Start () 
	{
		m_isAlive = false;
		m_direction = DIRECTION_UP;
		m_rotationAngle = 0.0f;
		m_isBlocked = false;

		m_tileWidth = GetComponent<SpriteRenderer>().sprite.rect.width;
		m_tileHeight = GetComponent<SpriteRenderer>().sprite.rect.height;

		for( int i = 1; i <= 10; i++ )
		{
			Missile missile = null;
			missile = Instantiate( m_missileObject, new Vector3(-1000.0f, -1000.0f, 0.0f), Quaternion.identity) as Missile;

			if( missile )
			{
				m_missilePod[i-1] = missile;
			}
			else
			{
				Debug.LogError("Missile not found!!!");
				break;
			}

		}
	
	}
	
	// Update is called once per frame
	void Update () 
	{
		if( m_isAlive && !m_isBlocked )
		{
			transform.position += m_direction * m_speed * Time.deltaTime;; 
			m_recoilTime -= Time.deltaTime;
			if( m_recoilTime < 0.0f )
				m_recoilTime = 0.0f;
		}


#if UNITY_EDITOR
		if( Input.GetKeyDown(KeyCode.RightArrow) )
		{
			ChangeDirection(Direction.Right);
		}
		else if( Input.GetKeyDown(KeyCode.LeftArrow) )
		{
			ChangeDirection(Direction.Left);
		}
		else if( Input.GetKeyDown(KeyCode.UpArrow) )
		{
			ChangeDirection(Direction.Up);
		}
		else if( Input.GetKeyDown(KeyCode.DownArrow) )
		{
			ChangeDirection(Direction.Down);
		}

		if( Input.GetKeyDown(KeyCode.Space) )
		{
			FireMissile();
		}

#endif
	}

	void FireMissile()
	{
		if( !m_isAlive )
			return;

		if( m_recoilTime == 0.0f )
		{
			m_recoilTime = 0.5f;

			for(int i = 0; i < 9; i++)
			{
				if( !m_missilePod[i].IsActive() )
				{
					m_missilePod[i].SetPosition(transform.position);
					m_missilePod[i].SetRotation(transform.rotation);
					m_missilePod[i].SetDirection(m_direction);
					m_missilePod[i].LaunchMissile();
					break;
				}
			}
		}
	}

	void ChangeDirection(Direction _direction)
	{
		m_isBlocked = false;
		switch (_direction)
		{
		case Direction.Left:
			m_rotationAngle = GetAngle(m_direction, DIRECTION_LEFT);
			m_direction = DIRECTION_LEFT;
			break;

		case Direction.Right:
			m_rotationAngle = GetAngle(m_direction, DIRECTION_RIGHT);
			m_direction = DIRECTION_RIGHT;
			break;

		case Direction.Up:
			m_rotationAngle = GetAngle(m_direction, DIRECTION_UP);
			m_direction = DIRECTION_UP;
			break;

		case Direction.Down:
			m_rotationAngle =  GetAngle(m_direction, DIRECTION_DOWN);
			m_direction = DIRECTION_DOWN;
			break;

		default:
			break;
		}
		transform.Rotate(new Vector3(0.0f, 0.0f, m_rotationAngle) );

	}

	float GetAngle(Vector3 _v1, Vector3 _v2)
	{
		float fRadians = 0.0f;

		fRadians = Mathf.Atan2(_v1.x*_v2.y - _v1.y*_v2.x, _v1.x*_v2.x+_v1.y*_v2.y);
		return Mathf.Rad2Deg * fRadians * 1.0f;
	}

	bool CheckBounds(Vector3 _position, float _limitX, float _limitY)
	{

		if( _position.x < 0 )
			return false;

		if( _position.x > _limitX )
			return false;

		if( _position.y < 0 )
			return false;

		if( _position.y  > _limitY )
			return false;

		return true; 
	}

	void OnCollisionEnter2D(Collision2D coll) 
	{
		///Debug.Log("Tank hit " + coll.gameObject.tag);
		if( coll.gameObject.tag == "Brick" || coll.gameObject.tag == "NonBreakableWall" )
		{
			//Collided with the tile, so stop motion 
			//of the tank
			m_isBlocked = true;
		}
	}

	public void SetPosition(Vector3 _position)
	{
		Vector3 pos = new Vector3( _position.x + m_tileWidth * 0.5f, _position.y + m_tileHeight * 0.5f, 0);
		transform.position = pos;
	}

	public void SetAlive(bool _alive)
	{
		m_isAlive = _alive;
	}

	void OnGUI()
	{
		if( GUI.Button(new Rect( 64.00f, Screen.height - 64.0f, 32.0f, 32.0f), ">" ) )
		{
			ChangeDirection(Direction.Right);
		}
		if( GUI.Button(new Rect( 8.0f, Screen.height - 64.0f, 32.0f, 32.0f), "<" ) )
		{
			ChangeDirection(Direction.Left);
		}
		if( GUI.Button(new Rect( 32.00f, Screen.height - 96.0f, 32.0f, 32.0f), "^" ) )
		{
			ChangeDirection(Direction.Up);
		}
		if( GUI.Button(new Rect( 32.00f, Screen.height - 32.0f, 32.0f, 32.0f), "v" ) )
		{
			ChangeDirection(Direction.Down);
		}

		if( GUI.Button(new Rect( Screen.width - 64.0f, Screen.height - 64.0f, 64.0f, 64.0f), "FIRE") )
		{
			FireMissile();
		}
	}
}
